<script setup lang="ts">
import * as echarts from 'echarts';
import {useResizeObserver} from '@vueuse/core'

let myChart: echarts.ECharts | null = null

const showEcharts = () => {
  const chartDom = document.getElementById('main2');
  myChart = echarts.init(chartDom);
  let option;

  option = {
    title: {
      text: 'Referer of a Website',
      subtext: 'Fake Data',
      left: 'center'
    },
    tooltip: {
      trigger: 'item'
    },
    legend: {
      orient: 'horizontal',
      bottom: 'bottom',
    },
    series: [
      {
        name: 'Access From',
        type: 'pie',
        radius: '50%',
        data: [
          {value: 1048, name: 'Search Engine'},
          {value: 735, name: 'Direct'},
          {value: 580, name: 'Email'},
          {value: 300, name: 'Video Ads'}
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  };

  option && myChart.setOption(option);

}

onMounted(() => {
  showEcharts()
})

// 监听视口大小 useResizeObserver 或者在 window.addEventListener('resize',callback) 中监听
const chartElRef = ref(null)
// 在浏览器宽度改变的时候，始终保持图表宽度是页面的 100%
useResizeObserver(chartElRef, () => myChart?.resize())
</script>

<template>
  <div id="main2" ref="chartElRef" w-full h-400px>

  </div>
</template>

<style scoped>

</style>